package com.hns.frame.util;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import com.hns.tool.pub.PubUtil;

/**
 * 对像数组排序
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
public class SortUtil {
	public static final int ASC = 1;

	public static final int DESC = -1;

	/**
	 * VOUtil 构造子注解。
	 */
	private SortUtil() {
		super();
	}

	/**
	 * 升序
	 * @param objs
	 * @param fields
	 * @author:Fisher
	 * @email:zhuangcaijin@126.com
	 */
	public static void ascSort(Object[] objs, String[] fields) {
		if (objs == null)
			return;
		
		if (fields == null || fields.length == 0)
			return;
		
		int[] ascFlags = new int[fields.length];
		java.util.Arrays.fill(ascFlags, SortUtil.ASC);
		
		sort(objs, fields, ascFlags);
	}
	
	public static void ascSort(List objs, String[] fields) {
		if (objs == null)
			return;
		
		if (fields == null || fields.length == 0)
			return;
		
		int[] ascFlags = new int[fields.length];
		java.util.Arrays.fill(ascFlags, SortUtil.ASC);
		
		sort(objs, fields, ascFlags);
	}

	public static void descSort(Object[] objs, String[] fields) {
		if (objs == null)
			return;
		if (fields == null || fields.length == 0)
			return;
		int[] ascFlags = new int[fields.length];
		java.util.Arrays.fill(ascFlags, SortUtil.DESC);
		sort(objs, fields, ascFlags);
	}
	
	public static void descSort(List objs, String[] fields) {
		if (PubUtil.isEmpty(objs))
			return;
		
		if (objs == null)
			return;
		if (fields == null || fields.length == 0)
			return;
		int[] ascFlags = new int[fields.length];
		java.util.Arrays.fill(ascFlags, SortUtil.DESC);
		sort(objs, fields, ascFlags);
	}


	public static String getCombinesKey(Object obj, String[] groupFields) {
		if (obj == null)
			throw new IllegalArgumentException(" obj cann't be null");
		if (groupFields == null || groupFields.length == 0)
			throw new IllegalArgumentException(" groupFields cann't be null or empty");
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < groupFields.length; i++) {
			result.append("" + ReflectUtil.getFieldValue(obj, groupFields[i]));
		}

		return result.toString();
	}

	static public void sort(Object[] vos, final String[] fields, final int[] ascFlags) {
		if (vos == null)
			return;
		if (fields == null || fields.length == 0)
			return;
		if (ascFlags == null) {
			throw new IllegalArgumentException("sort ascFlags cann't be null");
		} else if (fields.length != ascFlags.length) {
			throw new IllegalArgumentException(" length of fields not equal with that of ascFlags");
		}
		for (int i = 0; i < ascFlags.length; i++) {
			if (ascFlags[i] != ASC && ascFlags[i] != DESC)
				throw new IllegalArgumentException(" Illegal Value of ascFlag i=" + i + " value= "
						+ ascFlags[i]);
		}

		java.util.Comparator c = new java.util.Comparator() {
			public int compare(Object o1, Object o2) {

				int Greater = 1;
				int Less = -1;
				int Equal = 0;

				for (int i = 0; i < fields.length; i++) {
					Object v1 = ReflectUtil.getFieldValue(o1, fields[i]);
					Object v2 = ReflectUtil.getFieldValue(o2, fields[i]);

					if (v1 == null && v2 == null)
						continue;
					if (v1 == null && v2 != null)
						return ascFlags[i] * Less;
					if (v1 != null && v2 == null)
						return ascFlags[i] * Greater;

					Comparable c1 = null;
					Comparable c2 = null;

					if (v1 instanceof Comparable && v2 instanceof Comparable) {
						c1 = (Comparable) v1;
						c2 = (Comparable) v2;
					} else if (v1 instanceof BigDecimal && v2 instanceof BigDecimal) {
						BigDecimal u1 = (BigDecimal) v1;
						BigDecimal u2 = (BigDecimal) v2;
						if (u1.compareTo(u2) == 0)
							continue;
						else
							return u1.compareTo(u2) * ascFlags[i];

					} else {
						c1 = ("" + v1);
						c2 = ("" + v2);
					}

					if (c1.compareTo(c2) == 0)
						continue;
					else
						return c1.compareTo(c2) * ascFlags[i];
				}
				return Equal;
			}
		};

		
		java.util.Arrays.sort(vos, c);
	}

	
	static public void sort(List<Object> vos, final String[] fields, final int[] ascFlags) {
		if (vos == null)
			return;
		if (fields == null || fields.length == 0)
			return;
		if (ascFlags == null) {
			throw new IllegalArgumentException("sort ascFlags cann't be null");
		} else if (fields.length != ascFlags.length) {
			throw new IllegalArgumentException(" length of fields not equal with that of ascFlags");
		}
		for (int i = 0; i < ascFlags.length; i++) {
			if (ascFlags[i] != ASC && ascFlags[i] != DESC)
				throw new IllegalArgumentException(" Illegal Value of ascFlag i=" + i + " value= "
						+ ascFlags[i]);
		}

		java.util.Comparator c = new java.util.Comparator() {
			public int compare(Object o1, Object o2) {

				int Greater = 1;
				int Less = -1;
				int Equal = 0;

				for (int i = 0; i < fields.length; i++) {
					Object v1 = ReflectUtil.getFieldValue(o1, fields[i]);
					Object v2 = ReflectUtil.getFieldValue(o2, fields[i]);

					if (v1 == null && v2 == null)
						continue;
					if (v1 == null && v2 != null)
						return ascFlags[i] * Less;
					if (v1 != null && v2 == null)
						return ascFlags[i] * Greater;

					Comparable c1 = null;
					Comparable c2 = null;

					if (v1 instanceof Comparable && v2 instanceof Comparable) {
						c1 = (Comparable) v1;
						c2 = (Comparable) v2;
					} else if (v1 instanceof BigDecimal && v2 instanceof BigDecimal) {
						BigDecimal u1 = (BigDecimal) v1;
						BigDecimal u2 = (BigDecimal) v2;
						if (u1.compareTo(u2) == 0)
							continue;
						else
							return u1.compareTo(u2) * ascFlags[i];

					} else {
						c1 = ("" + v1);
						c2 = ("" + v2);
					}

					if (c1.compareTo(c2) == 0)
						continue;
					else
						return c1.compareTo(c2) * ascFlags[i];
				}
				return Equal;
			}
		};

		
		Collections.sort(vos, c);
	}
	
	
	static public void ascSort(List<String> vos) {
		if (vos == null)
			return;
		
		java.util.Comparator c = new java.util.Comparator() {
			public int compare(Object o1, Object o2) {
				String v1 = (String)o1;
				String v2 = (String)o2;
				return v1.compareTo(v2);
			}
		};
		Collections.sort(vos, c);
	}
	
	static public void descSort(List<String> vos) {
		if (vos == null)
			return;
		
		java.util.Comparator c = new java.util.Comparator() {
			public int compare(Object o1, Object o2) {
				String v1 = (String)o1;
				String v2 = (String)o2;
				int Greater = 1;
				int Less = -1;
				int Equal = 0;
				return v2.compareTo(v1);
			}
		};
		Collections.sort(vos, c);
	}
	
}